﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace CustomControlsNS
{
	/// <summary>
	/// Summary description for CustomButton
	/// </summary>
	public class CustomRepeater : CompositeControl
	{
		private bool bFormatHTML = false;

		public CustomRepeater()
		{
		}

        public CustomRepeater(string strText, string strTitle, string strOnClick, string strAccessKey, bool bAppendSpace)
		{
			this.Text = strText;
			this.Title = strTitle;
			this.OnClick = strOnClick;
			this.AccessKey = strAccessKey;
			this.AppendSpace = bAppendSpace;
		}

		public string Text
		{
			get { return (string)ViewState["Text"]; }
			set { ViewState["Text"] = value; }
		}

		public string Title
		{
			get { return (string)ViewState["Title"]; }
			set { ViewState["Title"] = value; }
		}

		public new string AccessKey
		{
			get { return (string)ViewState["AccessKey"]; }
			set { ViewState["AccessKey"] = value; }
		}

		public new bool Enabled
		{
			get
			{
				if (ViewState["Enabled"] == null)
					ViewState["Enabled"] = true;
				return (bool)ViewState["Enabled"];
			}
			set { ViewState["Enabled"] = value; }
		}

		public new string Width
		{
			get { return (string)ViewState["Width"]; }
			set { ViewState["Width"] = value; }
		}

		public bool AppendSpace
		{
			get { return (bool)(this.ViewState["AppendSpace"] ?? false); }
			set { this.ViewState["AppendSpace"] = value; }
		}

		public string OnClick
		{
			get { return (string)ViewState["OnClick"]; }
			set { ViewState["OnClick"] = value; }
		}

		public event EventHandler ServerClick;

		private string GetTabs(int nLevel)
		{
			if (bFormatHTML)
			{
				string s = "\t";
				for (int i = 0; i < nLevel; ++i)
					s += "\t";
				return s;
			}
			else
				return "";
		}

		protected override void CreateChildControls()
		{
			string strEnter = "";
			if (bFormatHTML)
				strEnter = "\n";

			string strButtonHTML = "<table border='1' cellpadding='0' cellspacing='0' id='tbl" + this.ClientID + "' title='" + this.Title + "";
			
			
			strButtonHTML += strEnter + "</table>";
			
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			//base.AddAttributesToRender(writer);
		}

		public virtual void OnServerClick(Object sender, EventArgs e)
		{
			if (ServerClick != null)
				ServerClick(sender, e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			AddLinkedStyleSheet(this.Page, "~/Styles/Button.css");
			ScriptManager.RegisterClientScriptInclude(Page, typeof(Page), "Scripts_Button_js", Page.ResolveClientUrl("~/Scripts/Button.js"));

			base.OnPreRender(e);
		}

		public void AddLinkedStyleSheet(Page page, string stylesheet)
		{
			bool bAdded = false;
			foreach (Control c in page.Header.Controls)
				if (c is HtmlLink && ((HtmlLink)c).Href == stylesheet)
				{
					bAdded = true;
					continue;
				}
			if (!bAdded)
			{
				HtmlLink link = new HtmlLink();
				link.Href = stylesheet;
				link.Attributes["rel"] = "stylesheet";
				link.Attributes["type"] = "text/css";

				page.Header.Controls.Add(link);
			}
		}

	}
}
